package com.lemner.motion.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.lemner.motion.R;
import com.lemner.motion.activity.LoginActivity;
import com.lemner.motion.activity.RunDetailActivity;
import com.lemner.motion.base.BaseFragment;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.bean.GetrecordBean;
import com.lemner.motion.util.MyDividerItemDecoration;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

import java.io.Serializable;
import java.util.List;

import retrofit2.http.POST;

/**
 * A simple {@link Fragment} subclass.
 */
public class SonHistFragment extends BaseFragment {
private TextView tv_month,tv_sum;
private RecyclerView rv_hist;
private List<GetrecordBean.DataBean>list;
    private  int i=0;
    private static final int EMPTY_VIEW = 1;

    public SonHistFragment() {
        // Required empty public constructor
    }


    @Override
    public int getLayoutFile() {
        return R.layout.fragment_son_hist;
    }

    public static SonHistFragment newInstance(String tabTitle) {
        Bundle args = new Bundle();
        SonHistFragment fragment = new SonHistFragment();
        args.putString("title", tabTitle);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
     tv_month=(TextView)view.findViewById(R.id.tv_month);
     tv_sum=(TextView)view.findViewById(R.id.tv_sum);
     rv_hist=(RecyclerView)view.findViewById(R.id.rv_hist);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        rv_hist.setLayoutManager(linearLayoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rv_hist.setHasFixedSize(true);
        rv_hist.addItemDecoration(new MyDividerItemDecoration(getActivity(),LinearLayoutManager.VERTICAL));
        i=getArguments().getInt("key",0);
        tv_month.setText((i+1) + "月");
    }
    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        getData(String.valueOf(i+1));
    }

    BaseListener<GetrecordBean>listener=new BaseListener<GetrecordBean>() {
        @Override
        public void onResponse(GetrecordBean getrecordBean) {
            if(getrecordBean.getErrorCode().equals("0000"))
                forceonline();
            else if(getrecordBean.getReturnCode().equals("0001")) {
                list=getrecordBean.getData();
                rv_hist.setAdapter(new HistAdapter(list));
            }
            else
                showToast(getrecordBean.getErrorMsg());
        }

        @Override
        public void onFail(String msg) {
                  showToast(msg);
        }
    };


    public void getData(String mouth){
        BaseModel<GetrecordBean> model=new BaseModel<>();
        retrofit2.Call<GetrecordBean>call=  model.service.getRecord(LoginActivity.studentid,LoginActivity.session,mouth);
        model.callEnqueue(call,listener);
    }

    class HistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        private List<GetrecordBean.DataBean>list;

        public HistAdapter(List<GetrecordBean.DataBean> list) {
            this.list = list;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View view;
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            if (viewType == EMPTY_VIEW) {
                 view = inflater.inflate(R.layout.rv_emtry, parent, false);
                return new EmtryHolder(view);
            } else {
                view = inflater.inflate(R.layout.rv_hist, parent, false);
                return new ViewHolder(view);
            }

        }

        @SuppressLint({"SetTextI18n", "DefaultLocale"})
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            if (holder instanceof ViewHolder) {
         ViewHolder viewHolder=(ViewHolder)holder;
         Picasso.with(getActivity()).load(list.get(position).getRecordImg()).placeholder(R.drawable.run).into(viewHolder.iv_waypic);
         tv_sum.setText(String.format("%.2f", list.get(position).getSumDistance())+"公里");
         viewHolder.tv_day.setText(list.get(position).getUploadTime().substring(8,10)+"日");
         long time=list.get(position).getUseTime();
         viewHolder.tv_sumtime.setText(String.format("%02d:%02d:%02d",  time/ 3600, time / 60 % 60, time % 60));
         viewHolder.tv_way.setText(list.get(position).getDistance()+"");
         viewHolder.ll_list.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 Intent intent=new Intent(getActivity(), RunDetailActivity.class);
                 Bundle bundle = new Bundle();
                 bundle.putSerializable("colors", (Serializable) list.get(position).getColorArray());
                 bundle.putSerializable("latLngs", (Serializable) list.get(position).getLocationArray());
                 intent.putExtras(bundle);
                 startActivity(intent);
             }
         });
 }
        }
        @Override
        public int getItemViewType(int position) {
            if (list.size() == 0) {
                return EMPTY_VIEW;
            }
            return super.getItemViewType(position);
        }

        @Override
        public int getItemCount() {
            return list.size() > 0 ? list.size() : 1;
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv_waypic,iv_right1;
        private TextView tv_day,tv_way,tv_sumtime;
        private LinearLayout ll_list;
        public ViewHolder(View itemView) {
            super(itemView);
            iv_waypic=(ImageView)itemView.findViewById(R.id.iv_waypic);
            iv_right1=(ImageView)itemView.findViewById(R.id.iv1_right);
            tv_day=(TextView)itemView.findViewById(R.id.tv_day);
            tv_way=(TextView)itemView.findViewById(R.id.tv_way);
            tv_sumtime=(TextView)itemView.findViewById(R.id.tv_sumtime);
            ll_list=(LinearLayout)itemView.findViewById(R.id.ll_list);
        }
    }

    class  EmtryHolder extends RecyclerView.ViewHolder{
          private  ImageView imageView;
          private TextView textView;
        public EmtryHolder(View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.iv_emtry);
            textView=(TextView)itemView.findViewById(R.id.tv_emtry);
        }
    }






}
